package com.ruoyi.web.controller.shopping.product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.view.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/xiaomi/v2/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private PictureService pictureService;

    @Autowired
    private CollectService collectService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping()
    public R<List<ProductDto>> selectAll(int pageNum, int pageSize, Integer categoryId, Integer productId, String productName){
        PageHelper.startPage(pageNum,pageSize);
        List<ProductDto> products = productService.selectAll(categoryId,productId,productName);
        for (ProductDto product : products) {
            System.out.println(product);
            Integer categoryId1 = product.getCategoryId();
            String s = categoryService.selectById(categoryId1);
            product.setCategoryName(s);
        }
        PageInfo<ProductDto> productPageInfo = new PageInfo<>(products);
        Long total = productPageInfo.getTotal();;
        return R.success(products,1,total);
    }

    @GetMapping("/hot")
    public R<List<Category>> hot(){
        List<Category> categories = categoryService.selectCategoryList();
        for (Category category : categories) {
            List<Product> products = productService.selectByDetail(category.getCategoryId(),null,null);
            category.setProducts(products);
        }
        PageInfo<Category> categoryPageInfo = new PageInfo<>(categories);
        return R.success(categories,1,categoryPageInfo.getTotal());
    }

    @GetMapping("/{productId}")
    public R<Product> getDetail(@PathVariable(value = "productId") Integer productId, HttpServletRequest request){
        Product product = productService.getAll(null, productId, null);
        String user_id = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("token")){
                System.out.println(cookie.getValue());
                user_id = cookie.getValue();
            }
        }
        List<CollectDto> collect = collectService.getCollect(Integer.valueOf(user_id),productId);
        for (CollectDto collectDto : collect) {
            Integer productId1 = collectDto.getProductId();
            if (product.getProductId() == productId1){
                product.setIsCollect(1);
            }
        }

        List<Picture> pictures = pictureService.getAll(null, product.getProductId());
        product.setPictures(pictures);
        return R.success(product);
    }


}
